package ylg.cn.com.welecome.littlered.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import ylg.cn.com.welecome.littlered.WelcomAnimFragment;

/**
 * Created by Administrator on 2017/11/13.
 */

public class ParentViewPager extends ViewPager {

    public static boolean mLoginPageLock = false;

    private float mCx,mCy;

    private int[] mTvSkipLocation;
    private Boolean mSkipFlag = true;
    private int margin, left, top, right, bottom;

    public ParentViewPager(Context context) {
        this(context, null);
    }

    public ParentViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private WelcomAnimFragment mWelcomAnimFragment;

    public void setWelcomAnimFragment(WelcomAnimFragment welcomAnimFragment) {
        this.mWelcomAnimFragment = welcomAnimFragment;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (this.getCurrentItem()==1||mLoginPageLock){
            return true;
        }
        if (mWelcomAnimFragment!=null){
            mCx = getX();
            mCy = getY();
            if (mTvSkipLocation==null){
                mTvSkipLocation = new int[2];
                mWelcomAnimFragment.tv_skip.getLocationOnScreen(mTvSkipLocation);
            }
            if (left==0){//这是获得view的界面的有效面积;
                margin = DisplayUtil.dip2px(getContext(), 10);
                left = mTvSkipLocation[0] - margin;
                top = mTvSkipLocation[1] - margin;
                right = mTvSkipLocation[0] + mWelcomAnimFragment.tv_skip.getWidth() + margin;
                bottom = mTvSkipLocation[1] + mWelcomAnimFragment.tv_skip.getHeight() + margin;
            }
            if (mCx - left > 0 && right - mCx > 0 && mCy - top > 0 && bottom - mCy > 0 && !mLoginPageLock) {
            } else {//如果点到外面将无效;
                mSkipFlag = false;
            }
            if (ev.getAction() == MotionEvent.ACTION_UP) {//手指抬起的时候并且在有效范围内
                if (mCx - left > 0 && right - mCx > 0 && mCy - top > 0 && bottom - mCy > 0 && !mLoginPageLock && mSkipFlag) {
                    if (mWelcomAnimFragment.mWelcomAnimFragmentInterface != null) {
                        mWelcomAnimFragment.mWelcomAnimFragmentInterface.onSkip();
                    }
                }
                //回复初始化;
                mSkipFlag = true;
                mCx = 0;
                mCy = 0;
            }

            /**
             * touch事件由顶层viewpager捕捉,手动分发到两个子viewpager
             */
            if (mWelcomAnimFragment.imageViewPager != null && !mWelcomAnimFragment.imageViewPager.mIsLockScoll) {
                mWelcomAnimFragment.imageViewPager.onTouchEvent(ev);
            }
            if (mWelcomAnimFragment.textViewPager != null) {
                mWelcomAnimFragment.textViewPager.onTouchEvent(ev);
            }
            if (mWelcomAnimFragment.mIsMoveParent) {
                return super.onTouchEvent(ev);
            }
        }
        return true;
    }
}
